#!/usr/bin/env perl


# sub get_default_output_device() {
#     open(def_out, "apanel -print|");
#     while(<def_out>) {
# 	if(/Default Output:/) {
# 	    @id=split;
# 	    $id[6] =~ s/\)//;
# 	    $ResID=$id[6];
# 	}
#     }
#     close(def_out);
#     return $ResID;
# }
# 
# 
# 
#print &get_default_output_device,"\n";

#open(debug,">>/tmp/irixmixer.debug");

if(@ARGV) {
#    print debug "@ARGV\n";
#    print debug "arg1-> $ARGV[1]\n";
    @vols=split(':',$ARGV[1]),"\n";
    $leftvol=$vols[0];
    $rightvol=$vols[1];
#    print debug "vols => $leftvol:$rightvol\n";
    $leftgain=int($leftvol/5*4)-60;
    $rightgain=int($rightvol/5*4)-60;
#    print debug "*gain*",$leftgain,$rightgain,"\n";
    system("apanel -nodisplay -gain $leftgain $rightgain");
}


#close(debug);



open(gain, "apanel -print|");
while(<gain>) {
    if(/Headphone/) {
#    if(/Output Device.*res id $ResID/) {
	$isopen="open";
    }
	
    if(/Gain Range:/) {
	$isopen="close";
    }

    if($isopen eq "open") {
	if(/Gain:/) {
#	    print;
 	    s/^.*(-+[0-9]+)\..*,.*(-+[0-9]+)\..*/\1:\2/;
 	    chomp;
 	    @gains=split(':');
 	    $leftgain=$gains[0];
 	    $rightgain=$gains[1];
 
# 	    print "$leftgain,$rightgain\n";
 	    $leftvol=int(($leftgain+60)*5/4);
 	    $rightvol=int(($rightgain+60)*5/4);
 	    print "$leftvol:$rightvol\n";
	}
    }
}


# gain -60 => 0
# gain 21 => 100


